<script setup lang='ts'>

import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import{ updateUserInfoService } from '@/api/user'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()

const ruleFormRef = ref<FormInstance>()

const checkOldPass = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('Please input the old password'))
    }
    callback()
    // setTimeout(() => {
    //     if (!Number.isInteger(value)) {
    //         callback(new Error('Please input digits'))
    //     } else {
    //         if (value < 18) {
    //             callback(new Error('Age must be greater than 18'))
    //         } else {
    //             callback()
    //         }
    //     }
    // }, 1000)
}

const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('Please input the password'))
    } else {
        if (ruleForm.checkPass !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('checkPass', () => null)
        }
        callback()
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('Please input the password again'))
    } else if (value !== ruleForm.pass) {
        callback(new Error("Two inputs don't match!"))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    pass: '',
    checkPass: '',
    oldPass: '',
})

const rules = reactive<FormRules<typeof ruleForm>>({
    pass: [{ validator: validatePass, trigger: 'blur' }],
    checkPass: [{ validator: validatePass2, trigger: 'blur' }],
    oldPass: [{ validator: checkOldPass, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    console.log("submiting")
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            console.log('submit!')
            // 调用后端接口修改用户信息
            updateUserInfo()
        } else {
            console.log('error submit!')
            return false
        }
    })
}

const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

//新建一个对象用于存储提交到后端的信息,其中包括User和oldPassword
const toBackForm = ref({
    uid: '',
    phoneNumber: '', 
    password: '' ,
    oldPassword: ''
})

//调用后端接口修改用户信息
const updateUserInfo = async () => {

    console.log("开始修改密码")

    //数据拷贝从
    toBackForm.value.uid = userStore.user.uid
    toBackForm.value.phoneNumber = userStore.user.phoneNumber
    toBackForm.value.password = ruleForm.pass
    toBackForm.value.oldPassword = ruleForm.oldPass


    console.log("修改密码信息：" + toBackForm.value)
    let result = await updateUserInfoService(toBackForm.value)
    //输出结果看一下
    console.log("修改密码结果：" + result)

}

</script>

<template>
    <div class="container">
        <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" status-icon :rules="rules"
            label-width="auto" class="demo-ruleForm">
            <el-form-item label="OldPassword" prop="oldPass">
                <el-input v-model="ruleForm.oldPass" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="NewPassword" prop="pass">
                <el-input v-model="ruleForm.pass" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="Confirm" prop="checkPass">
                <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<style scoped>
.container {
    display: flex;
    justify-content: center;
    /* align-items: center; */
    margin: 10px;
    padding: 10px;
    width: 100%;
    height: 100%;
}
</style>